#! /bin/bash
# update by shang.kun
# date 20200417
# Use to set threshold trigger sensor warning

function help_info() {
more << EOF
    -h|--help   cat help info
    -v          check $0 version
    -m          add test mode
                  **********************************************************
                  lnr_a/lnr_d :  Lower Non Recoverable asserted/deasserted
                  lcr_a/lcr_d :  Lower Critical asserted/deasserted
                  lnc_a/lnc_d :  Lower Lower Non-Critical/deasserted
                  unr_a/unr_d :  Upper Non Recoverable asserted/deasserted
                  ucr_a/ucr_d :  Upper Critical asserted/deasserted
                  unc_a/unc_d :  Upper Lower Non-Critical/deasserted
                  *********************************************************
    --sdr       add sensor name
EOF
exit

}

function check_sensor() {
    sensor=`$IPMI sensor list | grep "^$sdrnm " | wc -l` 
    if [  $sensor -ne 0 ];then
	sleep 0.1
    else
	echo '##please check input sensor name wethear correct!##'
 	exit 1
    fi
}

#触发asserted时,得到所有的sensor的阈值,是为了判定sensor的值是否为NA，是否需要设置
function get_sensor_thresh_asserted() {
    cat /dev/null > tmp.log
    $IPMI sensor list | grep "^$sdrnm " | awk -F '|' '{print $5$6$7$8$9$10}' | sed -r 's/ +/ /g' >> tmp.log
    lnrthresh=`cat tmp.log | awk '{print $1}'`
    lcrthresh=`cat tmp.log | awk '{print $2}'`
    lncthresh=`cat tmp.log | awk '{print $3}'`
    uncthresh=`cat tmp.log | awk '{print $4}'`
    ucrthresh=`cat tmp.log | awk '{print $5}'`
    unrthresh=`cat tmp.log | awk '{print $6}'`
    threshold=($lnrthresh $lcrthresh $lncthresh $uncthresh $ucrthresh $unrthresh)
}

#触发deasserted,需使用原本所有的阈值;所以使用在asserted之前得到的阈值
function get_sensor_thresh_deasserted() {
    lnrthresh=`cat tmp.log | awk '{print $1}'`
    lcrthresh=`cat tmp.log | awk '{print $2}'`
    lncthresh=`cat tmp.log | awk '{print $3}'`
    uncthresh=`cat tmp.log | awk '{print $4}'`
    ucrthresh=`cat tmp.log | awk '{print $5}'`
    unrthresh=`cat tmp.log | awk '{print $6}'`
    threshold=($lnrthresh $lcrthresh $lncthresh $uncthresh $ucrthresh $unrthresh)
}

function Sensor_value() {
    sensor_value=`$IPMI sensor list | grep "^$sdrnm " | awk -F '|' '{print $2}'`
    temp_sensor=`echo $sdrnm | grep Temp | wc -l`
    if [ $temp_sensor -eq 0 ];then
        sensor_value_0=`echo "$sensor_value+0.002"|bc`
        sensor_value_1=`echo "$sensor_value+0.004"|bc`
        sensor_value_2=`echo "$sensor_value+0.006"|bc`
        sensor_value_3=`echo "$sensor_value-0.006"|bc`
        sensor_value_4=`echo "$sensor_value-0.004"|bc`
        sensor_value_5=`echo "$sensor_value-0.002"|bc`
    else    
        sensor_value_0=`echo "$sensor_value+3.000"|bc`
        sensor_value_1=`echo "$sensor_value+5.000"|bc`
        sensor_value_2=`echo "$sensor_value+6.000"|bc`
        sensor_value_3=`echo "$sensor_value-6.000"|bc`
        sensor_value_4=`echo "$sensor_value-5.000"|bc`
        sensor_value_5=`echo "$sensor_value-3.000"|bc`
    fi
}

function type_value (){
    case $i in
        0)
            t='lnr'
            v=${threshold[0]}
            value=$sensor_value_0;; #value 定义asserted的时候设置的值
        1)
            t='lcr'
            v=${threshold[1]}
            value=$sensor_value_1;; 
        2)
            t='lnc'
            v=${threshold[2]}
            value=$sensor_value_2;; 
        3)
            t='unc'
            v=${threshold[3]}
            value=$sensor_value_3;;
        4)
            t='ucr'
            v=${threshold[4]}
            value=$sensor_value_4;;
        5)
            t='unr'
            v=${threshold[5]}
            value=$sensor_value_5;;
    esac
}


function lnr_trigger() {
    if [ "`$IPMI sensor list | grep "^$sdrnm " | awk -F '|' '{print $5}' | sed -r 's/ +//g'`" == "na" ];then
        echo "sensor $sdrnm Lower-Non-Recoverable is na"
        exit 1
    else
       sleep 0.1
    fi
    if [ "$test_mode" == "lnr_a" ];then
        get_sensor_thresh_asserted
        Sensor_value
        i=2
        log_tag=0                #此变量用于后面check sel log 是否正确
        while [ $i -ge 0 ]
        do
            type_value
        if [ "$v" != "na" ];then
            log_tag=$(($log_tag+1))
            $IPMI sensor thresh $sdrnm $t $value
        else
            sleep 0.1
        fi
        i=$(($i-1))
        done
    else
	    get_sensor_thresh_deasserted
        i=0
        log_tag=0
        while [ $i -le 2 ]
        do
            type_value
        if [ "$v" != "na" ];then
            $IPMI sensor thresh $sdrnm $t $v
            log_tag=$(($log_tag+1))
        else
            sleep 0.1
        fi
        i=$(($i+1))
	done
    fi        
}

function lcr_trigger() {
    if [ "`$IPMI sensor list | grep "^$sdrnm " | awk -F '|' '{print $6}' | sed -r 's/ +//g'`" == 'na' ];then
        echo "sensor $sdrnm Lower-Critical is na"
        exit 1
    else
       sleep 0.1
    fi
    if [ "$test_mode" == "lcr_a" ];then
        get_sensor_thresh_asserted
        Sensor_value
        i=2
        log_tag=0
        while [ $i -ge 1 ]
        do
            type_value
        if [ "$v" != "na" ];then
            $IPMI sensor thresh $sdrnm $t $value
            log_tag=$(($log_tag+1))
        else
            sleep 0.1
        fi
        i=$(($i-1))
        done
    else
        get_sensor_thresh_deasserted
        i=1
        log_tag=0
        while [ $i -le 2 ]
        do
            type_value
        if [ "$v" != "na" ];then
            $IPMI sensor thresh $sdrnm $t $v
            log_tag=$(($log_tag+1))
        else
            sleep 0.1
        fi
        i=$(($i+1))
	done
    fi
}

function lnc_trigger() {
    if [ "`$IPMI sensor list | grep "^$sdrnm " | awk -F '|' '{print $7}' | sed -r 's/ +//g'`" == 'na' ];then
        echo "sensor $sdrnm Lower-Non-Critical is na"
        exit 1
    else
       sleep 0.1
    fi
    if [ "$test_mode" == "lnc_a" ];then
        get_sensor_thresh_asserted
        Sensor_value
        i=2
        log_tag=0
        type_value
        if [ "$v" != "na" ];then
            $IPMI sensor thresh $sdrnm $t $value
	    log_tag=$(($log_tag+1))
        else
            sleep 0.1
        fi
    else
        get_sensor_thresh_deasserted
        i=2
        log_tag=0
        type_value
        if [ "$v" != "na" ];then
            $IPMI sensor thresh $sdrnm $t $v
	    log_tag=$(($log_tag+1))
        else
            sleep 0.1
        fi
    fi
}


function unr_trigger() {
    if [ "`$IPMI sensor list | grep "^$sdrnm " | awk -F '|' '{print $10}' | sed -r 's/ +//g'`" == 'na' ];then
        echo "sensor $sdrnm upper-Non-Recoverable is na"
        exit 1
    else
       sleep 0.1
    fi
    if [ "$test_mode" == "unr_a" ];then
        get_sensor_thresh_asserted
        Sensor_value
        i=3
        log_tag=0
        while [ $i -le 5 ]
        do
            type_value
        if [ "$v" != "na" ];then
            $IPMI sensor thresh $sdrnm $t $value
            log_tag=$(($log_tag+1))
        else
            sleep 0.1
        fi
        i=$(($i+1))
        done
    else
        get_sensor_thresh_deasserted
        i=5
        log_tag=0
        while [ $i -ge 3 ]
        do
            type_value
        if [ "$v" != "na" ];then
            $IPMI sensor thresh $sdrnm $t $v
            log_tag=$(($log_tag+1))
        else
            sleep 0.1
        fi
        i=$(($i-1))
	done
    fi
}

function ucr_trigger() {
    if [ "`$IPMI sensor list | grep "^$sdrnm " | awk -F '|' '{print $9}' | sed -r 's/ +//g'`" == 'na' ];then
        echo "sensor $sdrnm upper-Critical is na"
        exit 1
    else
       sleep 0.1
    fi
    if [ "$test_mode" == "ucr_a" ];then
        get_sensor_thresh_asserted
        Sensor_value
        i=3
        log_tag=0
        while [ $i -le 4 ]
        do
            type_value
        if [ "$v" != "na" ];then
            $IPMI sensor thresh $sdrnm $t $value
            log_tag=$(($log_tag+1))
        else
            sleep 0.1
        fi
        i=$(($i+1))
        done
    else
        get_sensor_thresh_deasserted
        i=4
        log_tag=0
        while [ $i -ge 3 ]
        do
            type_value
        if [ "$v" != "na" ];then
            log_tag=$(($log_tag+1))
            $IPMI sensor thresh $sdrnm $t $v
        else
            sleep 0.1
        fi
        i=$(($i-1))
	done
    fi
}

function unc_trigger() {
    if [ "`$IPMI sensor list | grep "^$sdrnm " | awk -F '|' '{print $8}' | sed -r 's/ +//g'`" == 'na' ];then
        echo "sensor $sdrnm upper-Non-Critical is na"
        exit 1
    else
       sleep 0.1
    fi
    if [ "$test_mode" == "unc_a" ];then
        get_sensor_thresh_asserted
        Sensor_value
        i=3
        log_tag=0
        type_value
        if [ "$v" != "na" ];then
            $IPMI sensor thresh $sdrnm $t $value
	    log_tag=$(($log_tag+1))
        else
            sleep 0.1
        fi
    else
        get_sensor_thresh_deasserted
        i=3
        log_tag=0
        type_value
        if [ "$v" != "na" ];then
            $IPMI sensor thresh $sdrnm $t $v
	    log_tag=$((log_tag+1))
        else
            sleep 0.1
        fi
    fi
}

function sdr_info() {
    echo " "
    echo “check sensor status and value after modify sensor threshold”
    echo "**************************************************************************************************************************"
    cat /dev/null > sdr.log
    $IPMI sdr elist | grep -i "^$sdrnm " | tee -a sdr.log
    cat /dev/null > sensor.log
    $IPMI sensor list | grep -i "^$sdrnm " | tee -a sensor.log
    echo "***************************************************************************************************************************"
    sdr_status=`cat sdr.log | awk -F '|' '{print $3}' | sed 's/ //g'`
    sensor_status=`cat sensor.log | awk -F '|' '{print $4}' | sed 's/ //g'`
    case $test_mode in
        lnr_a)
            if [ "$sdr_status" == "lnr" ] && [ "$sensor_status" == "nr" ];then
                 printf "%-50s%10s\n" "The sdr status of sensor $sdrnm trigger $testmode :" "Pass"
            else
                 printf "%-50s%10s\n" "The sdr status of sensor $sdrnm trigger $testmode :" "Fail"
            fi
            ;;
        lcr_a)
            if [ "$sdr_status" == "lcr" ] && [ "$sensor_status" == "cr" ];then
                 printf "%-50s%10s\n" "The sdr status of sensor $sdrnm trigger $testmode :" "Pass"
            else
                 printf "%-50s%10s\n" "The sdr status of sensor $sdrnm trigger $testmode :" "Fail"
            fi
            ;;
        lnc_a)
            if [ "$sdr_status" == "lnc" ] && [ "$sensor_status" == "nc" ];then
                 printf "%-50s%10s\n" "The sdr status of sensor $sdrnm trigger $testmode :" "Pass"
            else
                 printf "%-50s%10s\n" "The sdr status of sensor $sdrnm trigger $testmode :" "Fail"
            fi
            ;;
        unr_a)
            if [ "$sdr_status" == "unr" ] && [ "$sensor_status" == "nr" ];then
                 printf "%-50s%10s\n" "The sdr status of sensor $sdrnm trigger $testmode :" "Pass"
            else
                 printf "%-50s%10s\n" "The sdr status of sensor $sdrnm trigger $testmode :" "Fail"
            fi
            ;;
        ucr_a)
            if [ "$sdr_status" == "ucr" ] && [ "$sensor_status" == "cr" ];then
                 printf "%-50s%10s\n" "The sdr status of sensor $sdrnm trigger $testmode :" "Pass"
            else
                 printf "%-50s%10s\n" "The sdr status of sensor $sdrnm trigger $testmode :" "Fail"
            fi
            ;;
        unc_a)
            if [ "$sdr_status" == "unc" ] && [ "$sensor_status" == "nc" ];then
                 printf "%-50s%10s\n" "The sdr status of sensor $sdrnm trigger $testmode :" "Pass"
            else
                 printf "%-50s%10s\n" "The sdr status of sensor $sdrnm trigger $testmode :" "Fail"
            fi
            ;;
        *)
            if [ "$sdr_status" == "ok" ] && [ "$sensor_status" == "ok" ];then
                 printf "%-50s%10s\n" "The sdr status of sensor $sdrnm trigger $testmode :" "Pass"
            else
                 printf "%-50s%10s\n" "The sdr status of sensor $sdrnm trigger $testmode :" "Fail"
            fi
            ;;
    esac

}

function check_sel() {
    echo "check sel log after modify sensor thresh"
    cat /dev/null > sel.log
    $IPMI sel elist | tee -a sel.log
    case $test_mode in 
        lnr_a|lnr_d)
            log=('Lower\ +Non-recoverable\ +going\ +low' 'Lower\ +Critical\ +going\ +low' 'Lower\ +Non-critical\ +going\ +low')
            if [ "$test_mode" == "lnr_a" ];then
                log_m=Asserted
            else
                log_m=Deasserted
            fi
            ;;
        lcr_a|lcr_d)
            log=('Lower\ +Critical\ +going\ +low' 'Lower\ +Non-critical\ +going\ +low')
            if [ "$test_mode" == "lcr_a" ];then
                log_m=Asserted
            else
                log_m=Deasserted
            fi
            ;;
        lnc_a|lnc_d)
            log=('Lower\ +Non-critical\ +going\ +low')
            if [ "$test_mode" == "lnc_a" ];then
                log_m=Asserted
            else
                log_m=Deasserted
            fi
            ;;
        unr_a|unr_d)
            log=('Upper\ +Non-recoverable\ +going\ +high' 'Upper\ +Critical\ +going\ +high' 'Upper\ +Non-critical\ +going\ +high')
            if [ "$test_mode" == "unr_a" ];then
                log_m=Asserted
            else
                log_m=Deasserted
            fi
            ;;
        ucr_a|ucr_d)
            log=('Upper\ +Critical\ +going\ +high' 'Upper\ +Non-critical\ +going\ +high')
            if [ "$test_mode" == "ucr_a" ];then
                log_m=Asserted
            else
                log_m=Deasserted
            fi
	    ;;
        unc_a|unc_d)
            log=('Upper\ +Non-Critical\ +going\ +high')
            if [ "$test_mode" == "unc_a" ];then
                log_m=Asserted
            else
                log_m=Deasserted
            fi
            ;;
    esac
    if [ $log_tag -eq 3  ];then
        id=0
        sel_count=0
        while [ $id -le 2  ]
	do
            sel_record=`cat sel.log | grep -i "$sdrnm" | egrep -i "${log[$id]}" | grep -i "$log_m" | wc -l`
            sel_count=$(($sel_count+$sel_record))
            id=$(($id+1))
        done
    elif [ $log_tag -eq 2 ]; then
        id=0
	sel_count=0
        while [ $id -le 1  ]
	do
            sel_record=`cat sel.log | grep -i "$sdrnm"| egrep -i "${log[$id]}" | grep -i "$log_m" | wc -l`
            sel_count=$(($sel_count+$sel_record))
            id=$(($id+1))
        done
    else
	id=0
        sel_count=0
        sel_record=`cat sel.log | grep -i "$sdrnm"| egrep -i "${log[$id]}" | grep -i "$log_m" | wc -l`
        sel_count=$(($sel_count+$sel_record))
    fi
    if [ $log_tag -eq $sel_count ];then
        printf "%-50s%10s\n" "The sel of sensor $sdrnm trigger $testmode :" "Pass"
    else
        printf "%-50s%10s\n" "The sel of sensor $sdrnm trigger $testmode :" "Fail"
    fi

}

function main() {
    check_sensor
    $IPMI sel clear > /dev/null
    if [ "$test_mode" == "lnr_a" ] || [ "$test_mode" == "lnr_d" ] ;then
        lnr_trigger
        sleep 1
        sdr_info
	sleep 1
	check_sel
    elif [ "$test_mode" == "lcr_a" ] || [ "$test_mode" == "lcr_d" ] ;then
        lcr_trigger
        sleep 1
        sdr_info
	sleep 1
	check_sel
    elif [ "$test_mode" == "lnc_a" ] || [ "$test_mode" == "lnc_d" ] ;then
        lnc_trigger 
        sleep 1
        sdr_info
	sleep 1
	check_sel
    elif [ "$test_mode" == "unr_a" ] || [ "$test_mode" == "unr_d" ] ;then
        unr_trigger
        sleep 1
        sdr_info
	sleep 1
	check_sel
    elif [ "$test_mode" == "ucr_a" ] || [ "$test_mode" == "ucr_d" ] ;then
        ucr_trigger
        sleep 1
        sdr_info
	sleep 1
	check_sel
    elif [ "$test_mode" == "unc_a" ] || [ "$test_mode" == "unc_d" ] ;then
        unc_trigger 
        sleep 1
        sdr_info
        sleep 1
        check_sel
    else
        echo "please run $0 -h/--help to check help info" 
        exit 1       
    fi
}

Version="1.1"
IPMI=ipmitool

if [ $# -eq 0 ];then # def parameter
    echo "please run $0 -h/--help to check help info"        
    exit 1
else
    while [ "$1" != "" ]
    do
        case $1 in
	    -h|--help)
	        help_info;;
            --sdr)
                shift
                sdrnm=$1;;
            -m)
                shift
                test_mode=$1;;
	    -v)
		echo "script version : $Version"
                exit 1;;
             *)
                help_info;;
	    esac
	    shift
        done
fi

main 
